package com.service.lotterySystem.controller;

import com.service.config.annotation.Distinct;
import com.service.config.annotation.DistinctSmsCode;
import com.service.config.utils.*;
import com.service.lotterySystem.dto.SmsCode;
import com.service.lotterySystem.dto.UserInfoDto;
import com.service.lotterySystem.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;
/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 17:20
 * @description：
 */
@RestController
@RequestMapping("userInfoController")
@Api(value = "用户接口", tags = "用户接口")
public class UserInfoController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户根据手机号登录
     * @param
     * @return
     */
    @PostMapping(value = "queryByPhone",produces="application/json")
    @ApiOperation(value = "用户根据手机号登录")
    @ResponseBody
    @DistinctSmsCode
    public Result queryByPhone(@RequestBody SmsCode smsCode
            ,@RequestParam(value = "shareId",required = false) String shareId
            ,@RequestParam(value = "uScanTime",required = false) String uScanTime) throws Exception {
        if (!StringUtils.isEmpty(smsCode.getCode())) {
            return userInfoService.queryByPhone(smsCode,shareId,uScanTime);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }
    /**
     * 用户根据手机号发送短信验证码
     * @param
     * @return
     */
    @PostMapping(value = "SendSms",produces="application/json")
    @ApiOperation(value = "用户根据手机号发送短信验证码")
    @ResponseBody
    @Distinct
    public Result SendSms(@RequestBody SmsCode smsCode) throws Exception {
        if (StringUtils.isEmpty(smsCode.getTel())) {
            return Result.failure(DATA_FORMAT_WRONG,INCORRECT_DATA_FORMAT);
        }
        if (RegexUtil.isMobileSimple(smsCode.getTel())) {
            return userInfoService.sendSms(smsCode);
        }
        return Result.failure(USERNAME_Off_SITE,usernameOffSite);
    }
    /**
     * 根据用户id查询用户基本信息
     * @param
     * @return
     */
    @GetMapping(value = "queryUser",produces="application/json")
    @ApiOperation(value = "根据用户id查询用户基本信息（舍弃）")
    @ResponseBody
    public Result queryUser(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                    @RequestParam(value = "uid", required = false)String uid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return userInfoService.queryUserById(uid);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 根据用户id查询用户资料信息
     * @param
     * @return
     */
    @GetMapping(value = "queryUserDetail",produces="application/json")
    @ApiOperation(value = "根据用户id查询用户资料信息")
    @ResponseBody
    public Result queryUserDetail(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                  @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                                  @RequestParam(value = "uid",required = false) @ApiParam("card为1时，则传uid") String uid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card == 1) {
                    return userInfoService.queryUserDetail(uid);
                }
                if (card == 0) {
                    return userInfoService.queryUserDetail(userId);
                }
                return Result.failure(USERNAME_Off_SITE,"参数错误");
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 修改用户信息
     * @param
     * @return
     */
    @PostMapping(value = "updateUserInfo",produces="application/json")
    @ApiOperation(value = "修改用户信息（舍弃）")
    @ResponseBody
    public Result updateUserInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                              @RequestBody UserInfoDto userInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return userInfoService.update(userInfoDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
}
